package rc.work.module.infra.service.onlyoffice.filetable;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import rc.work.framework.common.exception.ErrorCode;
import rc.work.framework.common.pojo.PageResult;
import rc.work.framework.common.util.object.BeanUtils;

import rc.work.module.infra.controller.admin.onlyoffice.filetable.vo.FileTablePageReqVO;
import rc.work.module.infra.controller.admin.onlyoffice.filetable.vo.FileTableSaveReqVO;
import rc.work.module.infra.dal.dataobject.file.FileDO;
import rc.work.module.infra.dal.mysql.onlyoffice.filetable.FileTableMapper;
import rc.work.module.infra.util.CheckVariable;

import java.util.List;

import static rc.work.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 文件绑定表信息 Service 实现类
 *
 * @author 默认
 */
@Service
@Validated
public class FileTableServiceImpl implements FileTableService {

    private static final ErrorCode FILE_TABLE_NOT_EXISTS = new ErrorCode(5001, "文件不存在不存在");

    @Resource
    private FileTableMapper fileTableMapper;

//    @Resource
//    private JdzkClassicCaseMapper jdzkClassicCaseMapper;

    @Override
    public Long createFileTable(FileTableSaveReqVO createReqVO) {
        // 插入
        FileDO fileTable = BeanUtils.toBean(createReqVO, FileDO.class);


//        if(StringUtils.isBlank(fileTable.getBelongdept())){
//            Long deptId=getLoginUserDeptId();
//
//            fileTable.setBelongdept(String.valueOf(deptId));
//        }
//
//        if(StringUtils.isBlank(fileTable.getBelongdeptname())){
//            String belongdeptName = jdzkClassicCaseMapper.getDeptNameByDeptId(Long.valueOf(fileTable.getBelongdept()));
//            fileTable.setBelongdeptname(belongdeptName);
//        }


        fileTableMapper.insert(fileTable);
        // 返回
        return fileTable.getId();
    }

    @Override
    public void updateFileTable(FileTableSaveReqVO updateReqVO) {
        // 校验存在
        validateFileTableExists(updateReqVO.getId());
        // 更新
        FileDO updateObj = BeanUtils.toBean(updateReqVO, FileDO.class);
        fileTableMapper.updateById(updateObj);
    }

    @Override
    public void deleteFileTable(Long id) {
        // 校验存在
        validateFileTableExists(id);
        // 删除
        fileTableMapper.deleteById(id);
    }

    private void validateFileTableExists(Long id) {
        if (fileTableMapper.selectById(id) == null) {
            throw exception(FILE_TABLE_NOT_EXISTS);
        }
    }

    @Override
    public FileDO getFileTable(Long id) {
        return fileTableMapper.selectById(id);
    }

    @Override
    public PageResult<FileDO> getFileTablePage(FileTablePageReqVO pageReqVO) {
        return fileTableMapper.selectPage(pageReqVO);
    }

    @Override
    public FileDO downFile(Long id) {
        FileDO fileTableDO=fileTableMapper.selectById(id);
        if(!CheckVariable.isEmpty(fileTableDO)){
//            Long downcount = fileTableDO.getDownCount();
//            if(downcount!=null){
//                int count = downcount.intValue();
//                count=count+1;
//                fileTableDO.setDownCount(Long.valueOf(count));
//            }else {
//                fileTableDO.setDownCount(1L);
//            }
            fileTableMapper.updateById(fileTableDO);
        }

        return fileTableDO;
    }

    @Override
    public void deleteFileTableByTableIdAndTableName(Long tableId, String tableName) {
        fileTableMapper.deleteFileTableByTableIdAndTableName(tableId,tableName);
    }

    @Override
    public void deleteFileTableByIdNameType(Long tableId, String tableName, String tableType) {
        fileTableMapper.deleteFileTableByIdNameType(tableId,tableName,tableType);
    }

    @Override
    public List<FileDO> getFileTableAll() {
        return fileTableMapper.getFileTableAll();
    }

    @Override
    public List<FileDO> getFileTableInfo(String tableName, Long tableId) {
        return fileTableMapper.getFileTable(tableId,tableName);
    }

}
